package abc.com.abc.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import abc.com.abc.R;
import abc.com.abc.utils.AnalysisiUtils;
import abc.com.abc.utils.MD5Utils;

public class ModifyPsw_Activity extends AppCompatActivity {
    private TextView tv_main_title;
    private TextView tv_back;
    private EditText et_original_psw,et_new_psw,et_new_psw_again;
    private Button bt_save_psw;
    private String originalPSW,newPSW,newPswAgain;
    private  String userName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_psw);
        //设置竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        init();
        userName = AnalysisiUtils.readLoginUserName(this);
    }

    private void init() {
        tv_main_title = (TextView)findViewById(R.id.tv_main_title);
        tv_back = findViewById(R.id.tv_back);
        tv_main_title.setText("修改密码");
        et_original_psw = findViewById(R.id.et_original_psw);
        et_new_psw = findViewById(R.id.et_new_psw);
        et_new_psw_again =findViewById(R.id.et_new_psw_again);
        bt_save_psw = findViewById(R.id.btn_save_psw);
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ModifyPsw_Activity.this.finish();
            }
        });
        //保存
        bt_save_psw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getEditString();
                if (TextUtils.isEmpty(originalPSW)){
                    Toast.makeText(ModifyPsw_Activity.this,"请输入原始密码",Toast.LENGTH_SHORT).show();
                    return;
                }else if (!MD5Utils.md5(originalPSW).equals(readPsw())){
                    Toast.makeText(ModifyPsw_Activity.this,"输入的原始密码不一致",Toast.LENGTH_SHORT).show();
                    return;
                }else if (TextUtils.isEmpty(newPSW)){
                    Toast.makeText(ModifyPsw_Activity.this,"请输入新密码",Toast.LENGTH_SHORT).show();
                }else if (TextUtils.isEmpty(newPswAgain)){
                    Toast.makeText(ModifyPsw_Activity.this,"请再次输入新密码",Toast.LENGTH_SHORT).show();
                }else if (!newPSW.equals(newPswAgain)){
                    Toast.makeText(ModifyPsw_Activity.this,"两次新密码不一样",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(ModifyPsw_Activity.this,"设置成功",Toast.LENGTH_SHORT).show();
                    modifyPsw(newPSW);
                    Intent intent = new Intent(ModifyPsw_Activity.this,LoginActivity.class);
                    startActivity(intent);
                    SettingActivity.instance.finish();
                    ModifyPsw_Activity.this.finish();
                }

            }
        });
    }
/*修改登录成功之后的密码*/
    private void modifyPsw(String newPSW) {
        String md5Psw = MD5Utils.md5(newPSW);
        SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(userName,md5Psw);
        editor.commit();
    }
/*读取原始密码*/
    private String readPsw() {
        SharedPreferences sp = getSharedPreferences("loginInfo",MODE_PRIVATE);
        String spPSW = sp.getString(userName,"");
        return spPSW;
    }

    //获取字符串
    private void getEditString() {
        originalPSW = et_original_psw.getText().toString().trim();
        newPSW = et_new_psw.getText().toString().trim();
        newPswAgain=et_new_psw_again.getText().toString().trim();
    }
}
